import 'package:example/common/styles/theme.dart';
import 'package:flutter_colorpicker/flutter_colorpicker.dart';

import '../../common/common.dart';
import 'package:flutter/material.dart';
import 'package:getx_scaffold_ohos/getx_scaffold_ohos.dart';

import 'index.dart';

class ThemePage extends GetView<ThemeController> {
  const ThemePage({super.key});



  // 主视图
  Widget _buildView() {
    return <Widget>[
      ListTile(
        title: Text(TextKey.genSuiXiTong.tr),
        trailing: GlobalService.to.themeMode == ThemeMode.system
            ? const Icon(Icons.check)
            : null,
        onTap: () {
          changeThemeMode(ThemeMode.system);
        },
      ),
      ListTile(
        title: Text(TextKey.liangSeZhuTi.tr),
        trailing: GlobalService.to.themeMode == ThemeMode.light
            ? const Icon(Icons.check)
            : null,
        onTap: () {
          changeThemeMode(ThemeMode.light);
        },
      ),
      ListTile(
        title: Text(TextKey.anSeZhuTi.tr),
        trailing: GlobalService.to.themeMode == ThemeMode.dark
            ? const Icon(Icons.check)
            : null,
        onTap: () {
          changeThemeMode(ThemeMode.dark);
        },
      ),
      ListTile(
        title: Text('自定义颜色'),
        onTap: () {
          showDialog(
            context: Get.context!,
            builder: (context) => AlertDialog(
              title: const Text('选择颜色'),
              content: Column(
                mainAxisSize: MainAxisSize.min,
                children: [
                  ColorPicker(
                    pickerColor: getStringAsync('themeColor', defaultValue: '#149963').c,
                    onColorChanged: (color) => {
                      controller.themeColor = color
                    }, // 实时更新颜色
                  ),
                ],
              ),
              actions: [
                TextButton(
                  onPressed: () => Navigator.pop(context),
                  child: const Text('取消'),
                ),
                TextButton(
                  onPressed: () {
                    setValue('themeColor', controller.themeColor.toHex());
                    Get.changeTheme(AppTheme.light);
                    refreshAppui();
                    Get.back();
                  },
                  child: const Text('确定'),
                ),
              ],
            ),
          );

        },
      ),
    ].toListView(
      separator: const DividerX(),
    );
  }

  @override
  Widget build(BuildContext context) {
    return GetBuilder<ThemeController>(
      init: ThemeController(),
      id: 'theme',
      builder: (_) {
        return Scaffold(
          appBar: AppBar(title: Text(TextKey.zhuTi.tr), elevation: 1),
          body: SafeArea(
            child: _buildView(),
          ),
        );
      },
    );
  }
}
